package com.fxust.thread;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.fxust.common.CommonDefine;
import com.fxust.common.SqlMapper;
import com.fxust.dao.ThirdTableDao;
import com.fxust.dialog.BaseDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 图片上传线程类
 * @auto johnking
 * @create 2017-12-17 下午9:19
 **/
public class UpLoadPicThread extends Thread{

    private static Logger logger = LoggerFactory.getLogger(UpLoadPicThread.class);

    private List<File> listFiles = new ArrayList<File>();
    private String uuid;
    private String filePath;

    public UpLoadPicThread(String uuid,String filePath){
        this.uuid = uuid;
        this.filePath = filePath;
    }
    @Override
    public void run() {
        File files = new File(filePath);
        BaseDialog dialog = CommonDefine.asyncTrainMap.get(uuid);
        insertPicData(files,dialog);
        dialog.setDealProgressBarValue(100);

    }
    /**
     * 图片数据插入数据库
     * @author fgq 2017年11月24日 下午7:00:25
     * @param fileParm
     */
    private void insertPicData(File fileParm,BaseDialog dialog){
        String picSql = SqlMapper.insertPicSql;
        File[] fileList = fileParm.listFiles();
        int count = 0;
        for(File file : fileList){
            if(file.isFile()){
                listFiles.add(file);
                if(listFiles.size() >=100){
                    List<File> tempList = new ArrayList<File>();
                    tempList.addAll(listFiles);
                    int resultCount = ThirdTableDao.executeInsertSQL(listFiles, picSql);
                    count += resultCount;
                    dialog.setDealNumFieldValue(String.valueOf(count));
                    logger.info("本次入库数据量:"+resultCount);
                    listFiles.clear();
                }
            }else{
                insertPicData(file,dialog);
            }
        }
        if(listFiles.size() > 0){
            int resultCount = ThirdTableDao.executeInsertSQL(listFiles, picSql);
            count += resultCount;
            dialog.setDealNumFieldValue(String.valueOf(count));
            logger.info("本次入库数据量:"+resultCount);
        }
    }


}
